package com.xzy.sms.web.system.service;

import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.xzy.sms.web.system.bean.PermItem;
import com.xzy.sms.web.system.dao.PermUserDao;
import com.xzy.sms.web.system.dao.PermUsergroupDao;
import com.xzy.sms.web.system.entity.PermUser;
import com.xzy.sms.web.system.entity.PermUsergroup;
import com.xzy.sms.web.system.service.interfaces.IBussPerm;
import com.xzyframework.SpringContextHolder;

/**
 * 权限service类
 * @author macong
 * @since 1.0
 * @version 2014-10-15 macong
 */
// 默认将类中的所有public函数纳入事务管理.
@Component
@Transactional
public class PermService {
    
    /** PermUserDao */
    @Autowired
    private PermUserDao permUserDao;
    
    /** PermUsergroupDao */
    @Autowired
    private PermUsergroupDao permUsergroupDao;
    
    /** 业务权限 */
    private IBussPerm bussPerm;
    
    /**
     * IBussPerm
     * 
     * @return bussPerm
     */
    public IBussPerm getBussPerm() {
        if (null == bussPerm) {
            Map<String, IBussPerm> map = SpringContextHolder.getCtx().getBeansOfType(IBussPerm.class);
            if (!map.isEmpty()) {
                for (Entry<String, IBussPerm> e : map.entrySet()) {
                    bussPerm = e.getValue();
                    break;
                }
            }
            // bussPerm = SpringContextHolder.getBean(IBussPerm.class);
        }
        return bussPerm;
    }
    
    /**
     * 获取权限集合数据
     * 
     * @return 权限集合数据
     */
    public List<Map<String, Object>> getPermissionList() {
        List<Map<String, Object>> lst = Lists.newArrayList();
        List<PermItem> permItems = getAllPerm();
        Map<String, Map<String, List<Object>>> relationMap = getAllPermRelationMap();// 获取全部关系数据
        for (PermItem permItem : permItems) {
            Map<String, Object> mapItem = Maps.newHashMap();
            String permStr = permItem.getPermStr();
            mapItem.put("permStr", permStr);
            mapItem.put("name", permItem.getName());
            mapItem.put("remarks", permItem.getRemarks());
            // 设置关系数据
            mapItem.put("userRelations", relationMap.get("withUser").get(permStr));
            mapItem.put("usergroupRelations", relationMap.get("withRole").get(permStr));
            lst.add(mapItem);
        }
        return lst;
    }
    
    /**
     * 获取数据库中所有权限关系Map对象
     * 
     * @return Map
     */
    private Map<String, Map<String, List<Object>>> getAllPermRelationMap() {
        Map<String, Map<String, List<Object>>> mapAll = Maps.newHashMap();
        
        // 权限与角色的关系
        Map<String, List<Object>> mapRole = Maps.newHashMap();
        List<PermUsergroup> lstPermRole = permUsergroupDao.findAllPermUsergroup();
        for (PermUsergroup permRole : lstPermRole) {
            String permStr = permRole.getPermStr();
            List<Object> lstPermRoleTemp = mapRole.get(permStr);
            if (null == lstPermRoleTemp) {
                lstPermRoleTemp = Lists.newArrayList();
                mapRole.put(permStr, lstPermRoleTemp);
            }
            lstPermRoleTemp.add(permRole);
        }
        mapAll.put("withRole", mapRole);
        
        // 权限与用户的关系
        Map<String, List<Object>> mapUser = Maps.newHashMap();
        List<PermUser> lstPermUser = permUserDao.findAll();
        for (PermUser permUser : lstPermUser) {
            String permStr = permUser.getPermStr();
            List<Object> lstPermUserTemp = mapUser.get(permStr);
            if (null == lstPermUserTemp) {
                lstPermUserTemp = Lists.newArrayList();
                mapUser.put(permStr, lstPermUserTemp);
            }
            lstPermUserTemp.add(permUser);
        }
        mapAll.put("withUser", mapUser);
        
        return mapAll;
    }
    
    /**
     * 根据userId查询所有权限串
     * 
     * @param userId userId
     * @return 查询集合
     */
    @Transactional(readOnly = true)
    public List<String> findPermStrsByUserId(String userId) {
        List<String> permStrs = Lists.newArrayList();
        
        List<String> permStrs1 = permUserDao.findByUserId(userId);
        if (permStrs1 != null) {
            permStrs.addAll(permStrs1);
        }
        
        List<String> permStrs2 = permUsergroupDao.findPermStrsByUserId(userId);
        if (permStrs2 != null) {
            permStrs.addAll(permStrs2);
        }
        
        return permStrs;
    }
    
    /**
     * 获取所有权限项
     * 
     * @return 权限串集合
     */
    @Transactional(readOnly = true)
    public List<PermItem> getAllPerm() {
        if (null != getBussPerm()) {
            List<PermItem> lst = getBussPerm().getAllPerm();
            if (null != lst) {
                return lst;
            }
        }
        return Lists.newArrayList();
    }
}
